<?php

namespace app\common\service\OrderService;

use app\model\BbcModel\ChannelWx;
use app\model\BbcModel\CloudLog;
use think\Facade\Db;
use app\Normal;
use app\common\service\RequestService\Http;


/**
 * 对接云仓订单接口
 * @author linan
 */
class OrderCloudService
{
    /**
     * 重新推单至云仓
     * @param array $data
     * @param string $method
     * @return array
     */
    public function sendToStoB($data, $method)
    {
        $orderData = rawurlencode(json_encode($data));
        $data['cid'] = $data['cid'] ?? 0;
        $info = ChannelWx::instance()->info(['cid' => $data['cid']]);
        if (empty($info)) {
            return Normal::returnCode(Normal::FAIL,"S2B配置错误");
        }
        $c['appid'] = $info['stb_app_id'];
        $c['appsecret'] = $info['stb_app_secret'];
        $c['url'] = $info['stb_url'];
        $request = [
            'appid' => $c['appid'],
            'requestMethod' => $method,
            'requestData' => $orderData,
        ];
        ksort($request);
        $key = $c['appsecret'];
        $sign = strtoupper(md5(getSignContent($request) . '&key=' . $key));
        $request['sign'] = $sign;
        $res = Http::post($c['url'], $request);
        CloudLog::instance()->add(['requestMethod' => $method, 'requestData' => json_encode($data, JSON_UNESCAPED_UNICODE), 'response' => $res,'cd'=>time()]);
        $res = json_decode($res, true);
        if(is_null($res)){
            $res['code']=0;
            $res['msg']='云仓出现错误，请联系客服';
        }
        return $res;
    }

}

